package com.chenyun.system.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.chenyun.boot.datasource.page.PageUtils;
import com.chenyun.common.annotation.Page;
import com.chenyun.common.exceptions.BusinessException;
import com.chenyun.common.vo.PageResult;
import com.chenyun.system.mapper.SysTenantMapper;
import com.chenyun.system.mapper.SysTenantTypeMapper;
import com.chenyun.system.model.SysTenant;
import com.chenyun.system.model.SysTenantType;
import com.chenyun.system.param.TenantTypeParam;
import com.chenyun.system.service.ISysTenantService;
import com.chenyun.system.service.ISysTenantTypeService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;
import java.util.Objects;

/**
 * 租户类型
 *
 * @author: lonni
 * @CreateTime: 2023-12-06  20:11
 * @Description:
 */
@Service
@Slf4j
public class SysTenantTypeServiceImpl extends ServiceImpl<SysTenantTypeMapper, SysTenantType> implements ISysTenantTypeService {
    /**
     * 添加租户类型
     *
     * @param param
     * @return
     */
    @Override
    public Integer crateTenantType(TenantTypeParam param) {
        this.checkParam(param);
        SysTenantType sysTenantType = BeanUtil.copyProperties(param, SysTenantType.class);
        this.save(sysTenantType);
        return sysTenantType.getId();
    }

    @Override
    public Boolean updateTenantType(TenantTypeParam param) {
        this.checkParam(param);
        SysTenantType tenantType = this.getById(param.getId());
        if (tenantType == null) {
            throw new BusinessException("未找到租户类型数据!");
        }
        BeanUtil.copyProperties(param, tenantType);
        return this.saveOrUpdate(tenantType);

    }

    /**
     * 查询数据
     *
     * @param param
     * @return
     */
    @Page
    @Override
    public PageResult pageList(TenantTypeParam param) {
        LambdaQueryWrapper<SysTenantType> eq = Wrappers.lambdaQuery(SysTenantType.class)
                .eq(Objects.nonNull(param.getId()), SysTenantType::getId, param.getId())
                .eq(Objects.nonNull(param.getStatus()), SysTenantType::getStatus, param.getStatus());
        List<SysTenantType> list = this.list(eq);
        return PageUtils.getPageHelperByList(list);
    }

    /**
     * 查询列表 不分页
     *
     * @param param
     * @return
     */
    @Override
    public List<SysTenantType> list(TenantTypeParam param) {
        LambdaQueryWrapper<SysTenantType> eq = Wrappers.lambdaQuery(SysTenantType.class)
                .eq(Objects.nonNull(param.getId()), SysTenantType::getId, param.getId())
                .eq(Objects.nonNull(param.getStatus()), SysTenantType::getStatus, param.getStatus());
        return this.list(eq);

    }

    /**
     * 校验类型是否合法
     *
     * @param typeId
     */
    @Override
    public SysTenantType checkType(Integer typeId) {

        SysTenantType byId = this.getById(typeId);
        if (Objects.isNull(byId)) {
            throw new BusinessException("未找到租户类型数据!");
        }
        if (byId.getStatus() == 1) {
            throw new BusinessException("租户类型已停用!");
        }
        return byId;

    }

    /**
     * 未租户设置|修改权限
     *
     * @param typeId
     * @param menuIds
     * @return
     */
    @Override
    public Boolean authTenantType(Integer typeId, List<String> menuIds) {
        SysTenantType tenantType = this.getById(typeId);
        if (Objects.isNull(tenantType)) {
            throw new BusinessException("租户类型不存在!");
        }
        tenantType.setMenuIds(menuIds);
        return this.updateById(tenantType);

    }

    void checkParam(TenantTypeParam param) {
        if (StrUtil.isBlank(param.getName())) {
            throw new BusinessException("租户类型必填!");
        } else if (StrUtil.isBlank(param.getMenuIds())) {
            throw new BusinessException("租户菜单必填!");
        }

    }


}
